 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : MiniQQ2
 * Package  : net.solosky.miniqq
 * File     : Encryptor.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : 2010-4-10
 * License  : Apache License 2.0 
 */
package net.solosky.easyavatar.api.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 *
 * @author solosky <solosky772@qq.com>
 */
public class QQEncryptor
{
	
	public static String encrypt(String plain, String verify)
	{
		byte [] bytes = md5_3(plain.getBytes());
		String bytestr = byte2HexString(bytes);
		bytestr = bytestr + verify.toUpperCase();
		bytes = md5(bytestr.getBytes());
		
		return byte2HexString(bytes);
	}
	
	/**
	 * Md5 三次字节数组
	 * @param bytes
	 * @return
	 */
	private static byte[] md5_3(byte[] bytes)
	{
		bytes = md5(bytes);
		bytes = md5(bytes);
		bytes = md5(bytes);
		return bytes;
	}
	
	/**
	 * 计算一个字节数组的Md5值
	 * @param bytes
	 * @return
	 */
	private static byte[] md5(byte[] bytes)
	{
		 MessageDigest dist = null;
		 byte[] result  	= null; 
	        try {
		        dist = MessageDigest.getInstance("MD5");
		        result = dist.digest(bytes);
	        } catch (NoSuchAlgorithmException e) {
	        	throw new IllegalArgumentException(e);
	        }
	    	  return result;
	}
	
	
	/**
	 * 把字节数组转换为16进制表示的字符串
	 * @param b
	 * @return
	 */
   private static String byte2HexString(byte[] b) 
   {   
	   StringBuffer sb = new StringBuffer();
	   char[] hex = new char[] { 
	            '0', '1', '2', '3', '4', '5', '6', '7',
	            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
	    };
    	if(b == null)
    		return "null";
    	
    	int offset = 0;
    	int len    = b.length; 
    	
        // 检查索引范围
        int end = offset + len;
        if(end > b.length)
            end = b.length;
        
        sb.delete(0, sb.length());
        for(int i = offset; i < end; i++) {
            sb.append(hex[(b[i] & 0xF0) >>> 4])
            	.append(hex[b[i] & 0xF]);
        }
        return sb.toString();
    }
   
   /**
    * 计算GTK(gtk啥东东？)
    * @param skey
    * @return
    */
   public static String gtk(String skey){
	   int hash = 5381;
	   for(int i=0; i<skey.length(); i++){
		   hash += (hash<<5) + skey.charAt(i);
	   }
	   return Integer.toString(hash & 0x7fffffff) ;
   }

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		//xu1234
		//eszn
		//6030CE197E3D556479BEB0DD633BCF55
		//A9233A4F4E90A8B3B9523A2A1C61C1C9
		//bwgf
		System.out.println(encrypt("xu1234", "bwgf"));
		//skey=@x7R71Hvvw
		//1088372313
		System.out.println(gtk("@LEnrsNgrE"));
		
		System.out.println(System.currentTimeMillis()/1000+"");
		
	}

}
